package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderAndMember;
import com.itheima.service.OrderSettingListService;
import com.itheima.utils.SMSUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;

/**
 *  预约列表
 */
@Component
@RequestMapping("/orderSettingList")
public class OrderSettingListController {

    @Reference
    private OrderSettingListService orderSettingListService;

    //分页查询预约列表
    @RequestMapping("/findPage")
    @ResponseBody
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return orderSettingListService.pageQuery(queryPageBean);
    }

    /**
     * 新增预约
     * @param orderAndMember 实体类
     * @param setmealIds 套餐id
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Result add(@RequestBody OrderAndMember orderAndMember,Integer[] setmealIds){

        try {
            orderSettingListService.add(orderAndMember,setmealIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增预约用户失败");//新增预约失败
        }
        return new Result(true, "新增预约用户成功");//新增预约成功
    }

    /**
     * 手机预约
     * @param orderAndMember 实体类
     * @return
     */
    @RequestMapping("/submit")
    @ResponseBody
    public Result submit(@RequestBody OrderAndMember orderAndMember){

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        String orderOrder = simpleDateFormat.format(orderAndMember.getOrderDate());
        String phoneNumber = orderAndMember.getPhoneNumber();
        //预约成功，可以为用户发送短信
        try {
            SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE, phoneNumber, orderOrder);
            return new Result(true, MessageConstant.ORDER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDER_FAIL);
        }
    }



    /**
     * 根据order主键id单一/批量删除
     * @param orderIds order主键id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(@RequestBody Integer[] orderIds){
        try {
            orderSettingListService.delete(orderIds);
            return new Result(true, MessageConstant.DELETE_ORDER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ORDER_FAIL);
        }
    }

    /**
     * 根据电话号码(单一/批量)通知用户取消预约
     * @param phoneNumbers 电话号码(单一/批量)
     * @param userNames 用户姓名
     * @return
     */
    @RequestMapping("/phoneNumberMessage")
    @ResponseBody
    public Result phoneNumberMessage(@RequestBody String[] phoneNumbers,String[] userNames){
        if (phoneNumbers != null && userNames != null) {   //判断数值为空的情况
            for (int i = 0; i < phoneNumbers.length; i++) {
                try {
                    SMSUtils.sendShortMessage(SMSUtils.ORDER_CANCEL, phoneNumbers[i], userNames[i]);
                } catch (ClientException e) {
                    e.printStackTrace();
                    //未能短信通知返回客服电话通知
                    return new Result(false, MessageConstant.MESSAGE_ORDER_FAIL, phoneNumbers[i]);
                }
            }
        }
        return new Result(true, MessageConstant.MESSAGE_ORDER_SUCCESS);
    }

}
